package A_review.review10_30;

public class JiaoTi {
    public   volatile  String  CODE="A";

    public  void  printA(){
        synchronized (this){
            while (!CODE.equals("A")){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            System.out.println("A");
            CODE="B";
            notifyAll();
        }
    }
    public  void  printB(){
        synchronized (this){
            while (!CODE.equals("B")){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            System.out.println("B");
            CODE="C";
            notifyAll();
        }
    }
    public  void  printC(){
        synchronized (this){
            while (!CODE.equals("C")){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            System.out.println("C");

            CODE="A";
            notifyAll();
        }
    }

    public static void main(String[] args) {

        JiaoTi jiaoTi = new JiaoTi();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                jiaoTi.printA();
            }
        },"线程A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                jiaoTi.printB();
            }
        },"线程B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                jiaoTi.printC();
            }
        },"线程C").start();


    }




}
